/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiSHA.cpp
	Library for Secure Hash Algorihm stuff.

******************************************************************************/

#include "MaxsiEngine.h"

BeginMaxsiNamespace

LINK	MESTR* CalculateSHA1(BYTE* Buffer, size_t BufferLen)
{
	MAXSI_TODO("There are lots of unchecked memory allocations here!");

	CSHA1 sha1;
	sha1.Update((unsigned char*)Buffer,(unsigned int)BufferLen);
	sha1.Final();
	char* HashData = new char[60];
	memset(HashData,0,60);
	sha1.ReportHash(HashData, CSHA1::REPORT_HEX);
	char* FinalHash = new char[41];
	for (int I = 0; I < 20; I++)
	{
		memcpy(FinalHash+2*I,HashData+3*I,2);
	}
	FinalHash[40] = 0;
	delete[] HashData;

	MESTR* Result = CHAR2MESTR(FinalHash);

	delete[] FinalHash;

	return Result;
}

EndMaxsiNamespace
